const svgCaptcha = require("svg-captcha");
//验证码逻辑业务
const TestCode = {
	//生成验证码
	gen(req, res, next) {
		// 创建验证码对象
		var captcha = svgCaptcha.create({
		// var captcha = svgCaptcha.createMathExpr({
			color: true,
			noise: 0
		});
		// 将验证码文本保存到 session 中
	    req.session.captcha = captcha.text;
	    
	    res.json({
	    	res_code: 1,
	    	res_error: "",
	    	res_body: {
	    		data: captcha.data
	    	}
	    });
    },
	//校验验证码
	verify(req,res,next){
		const {testcode} = req.query;
		if (testcode.toUpperCase() === req.session.captcha.toUpperCase()) {
			res.json({
				res_code: 1,
		    	res_error: "",
		    	res_body: {
	    			valid:true
	    		}
			})
		}else{
			res.json({
				res_code: 1,
		    	res_error: "",
		    	res_body: {
	    			valid:false
	    		}
			});
		}
	}
}

module.exports = TestCode;              